
<template>
  <div style="margin-left: 30%">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
       
         <!-- <el-link type="primary" style="float:right" @click="register()">用户注册</el-link> -->
      </div>
      <div class="text item">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          size="medium"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名" prop="user">
            <el-input
              v-model.number="ruleForm.user"
              placeholder="手机号登录"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input
              type="password"
              v-model="ruleForm.pass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <!-- <el-form-item>
            <el-radio v-model="radio" label="1">用户登录</el-radio>
            <el-radio v-model="radio" label="2">管理员登录</el-radio>
          </el-form-item> -->
          <!-- <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
        ></el-input> -->
          <!-- </el-form-item> -->
          <!-- <el-form-item label="年龄" prop="age">
        <el-input v-model.number="ruleForm.age"></el-input>
      </el-form-item> -->
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >提交</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <reader-add-or-update v-if="readerAddOrUpdateVisable" ref="addOrUpdate"></reader-add-or-update>
  </div>
</template>
<script>
import cookie from "js-cookie";
import ReaderAddOrUpdate from '../manager-add-or-update';
export default {
  components: { ReaderAddOrUpdate },
  data() {
    var checkUser = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error("请输入数字值"));
        } else {
          if (value.toString().length != 11) {
           
            callback(new Error("手机号长度有误"));
          } else {
            callback();
          }
        }
      }, 1000);
    };

    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    // var validatePass2 = (rule, value, callback) => {
    //   if (value === "") {
    //     callback(new Error("请再次输入密码"));
    //   } else if (value !== this.ruleForm.pass) {
    //     callback(new Error("两次输入密码不一致!"));
    //   } else {
    //     callback();
    //   }
    // };
    return {
      readerAddOrUpdateVisable:false,
       radio: '1',
      ruleForm: {
        user: "",
        pass: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],

        user: [{ validator: checkUser, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/sys/login/adminLogin'),
            method: "post",
            data: this.$http.adornData(this.ruleForm, false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              let time = new Date(new Date().getTime() + 2 * 60 * 60 * 1000);
              cookie.set("token", data.token, { expires: time });
              this.$router.go(0);
            } else {
              console.log("登录失败");
              this.$message({
                type: "error",
                message: "登录失败",
              });
            }
            this.dataListLoading = false;
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    register(){
      this.readerAddOrUpdateVisable = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init();
      });
    },
  },
  created(){
     this.$message({
                type: "info",
                message: "请登录",
              });
  }
};
</script>
<style>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 480px;
}
</style>
